package dog.gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import constants.STATUS;
import containers.AppKey;

import dog.model.Model;

public class MenuBarPanel extends JPanel {
	
	/**
	 * What the hell is this thing supposed to do?
	 */
	
	private static final long serialVersionUID = -8523415516758978626L;
	private Model model;
	private JButton weekList;

	class DummyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
		}
		
	}
	
	public MenuBarPanel(Model model) {
		this.model = model;
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		DummyListener dummy = new DummyListener();
		
		JButton editAppBtn = new JButton("Edit Appointment");
		c.gridx = 1;
		editAppBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editApp();
			}
		});
		add(editAppBtn, c);
		
		JButton newAppBtn = new JButton("New Appointment");
		c.gridx = 2;
		newAppBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				newApp();
			}
		});
		add(newAppBtn, c);		
	
		JButton delAppBtn = new JButton("Delete Appointment");
		c.gridx = 4;
		delAppBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteApp();
			}
		});
		add(delAppBtn, c);
		
		JButton prevWeekBtn = new JButton("<-");
		c.gridx = 5;
		prevWeekBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				prevWeek();
			}
		});
		add(prevWeekBtn, c);
		
		weekList = new JButton();
		timeFrameChanged();
		c.gridx = 6;
		weekList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectWeek();
			}
		});
		add(weekList, c);
		
		JButton nextWeekBtn = new JButton("->");
		c.gridx = 7;
		nextWeekBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextWeek();
			}
		});
		add(nextWeekBtn, c);
	}
	
	protected void editApp()
		{
		viewApp();
		}

	public void newApp() {
		model.getGUI().goToAppView(null,null);
	}
	public void viewApp() {
		model.getGUI().goToAppView(model.getAppointment(model.getSelected()),null);
	}
	
	public void deleteApp() {
		AppKey selected = model.getSelected();
		if (selected != null) 
			{
			if (selected.getOwnerID() == model.getPersonID())
				model.gui_cancelAppointment(selected.getOwnerID(), selected.getAppointmentID(), JOptionPane.showInputDialog("Reason ",""));
			else
				{
				if(model.getAppointment(selected).getStatuses().get(model.getPersonID())==STATUS.ACCEPTED)
					model.gui_setAttendance(model.getPersonID(), selected.getOwnerID(), selected.getAppointmentID(), STATUS.LEFT, JOptionPane.showInputDialog("Reason for leaving: "));//Should check whether we were deleted in the mean time but whatever
				else
					model.gui_setAttendance(model.getPersonID(), selected.getOwnerID(), selected.getAppointmentID(), STATUS.DENIED, JOptionPane.showInputDialog("Reason for leaving: "));//Should check whether we were deleted in the mean time but whateve
				}
			}
		else JOptionPane.showMessageDialog(null, "No appointment selected!", "Error", JOptionPane.ERROR_MESSAGE);
	}
 	
	public void prevWeek() {
		int week = model.getShownWeek() - 1;
		int year  = model.getShownYear();
		if (week < 1) {
			week = 53;
			year = year - 1;
		}
		model.gui_setAccessRange(year, week);
		timeFrameChanged();
	}
	
	public void nextWeek() {
		int week = model.getShownWeek() + 1;
		int year  = model.getShownYear();
		if (week > 53) {
			week = 1;
			year = year + 1;
		}
		model.gui_setAccessRange(year, week);
		timeFrameChanged();
	}
	
	private void timeFrameChanged()
		{
		weekList.setText(model.getShownWeek()+"-"+model.getShownYear());
		}

	public void selectWeek() {
		model.gui_setAccessRange(Integer.parseInt(JOptionPane.showInputDialog("Year ","")), Integer.parseInt(JOptionPane.showInputDialog("Week ","")));
		timeFrameChanged();
	}
}
